<template>
  <div class="zh-examinationPaper">
    <nav-header></nav-header>
    <div class="zh-table">
      <div class="zh-table-open clear">
        <div class="zh-open-left fl">
          <div class="open-width" style="width: 120px" v-if="$store.state.platformInformation">
            <el-select v-model="platform_id" filterable placeholder="来源学校/教育局">
              <el-option
                v-for="item in platformList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </div>
          <div style="display: inline-block" v-else>
            <div class="open-width" style="width: 100px">
              <el-select v-model="listParam.study_section_id" filterable placeholder="请选择">
                <el-option
                  v-for="item in orgLevel2"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </div>
            <div class="open-width" style="width: 100px">
              <el-select v-model="listParam.grade_id" filterable placeholder="请选择">
                <el-option
                  v-for="item in orgLevel3"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </div>
            <div class="open-width" style="width: 100px">
              <el-select v-model="listParam.subject_id" filterable placeholder="请选择">
                <el-option
                  v-for="item in orgSubject"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </div>
            <div class="open-width" style="width: 100px">
              <el-select v-model="listParam.source_id" filterable placeholder="请选择">
                <el-option
                  v-for="item in sourceData"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </div>
          </div>
          <div class="open-width">
            <el-input v-model="listParam.keyword" placeholder="请输入试卷名搜索" @keyup.enter.native="searchFunc"></el-input>
          </div>
          <el-button type="primary" @click="searchFunc">搜索</el-button>
          <el-button type="primary" @click="reset">重置</el-button>
        </div>
        <div class="zh-open-right fr">
          <!-- <el-button type="text" v-if="!$store.state.checkLevel" @click="toLink('/resources/exam/examination-push-history')">推送历史</el-button>
          <el-button type="primary" v-if="$store.state.platformInformation" @click="pushLowAll">批量推下级</el-button>
          <el-button type="primary" v-if="platform && !$store.state.checkLevel" @click="pushUpAll">批量推上级</el-button> -->
          <el-button type="danger" @click="deleteMuilt_confirm">批量删除</el-button>
        </div>
      </div>
      <div class="zh-table-contents" v-loading="loading.status" :element-loading-text="loading.txt">
        <el-table
          :data="listData"
          fixed
          border
          max-height="600"
          @selection-change="handleSelectionChange"
          style="width: 100%"
        >
          <el-table-column
            type="selection"
            width="50"
            align="center"
            :selectable="selectable"
            fixed
          ></el-table-column>
          <el-table-column prop="name" label="试卷标题" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column prop="source" label="类型" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column prop="grade" label="年级" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column prop="subject" label="学科" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column prop="_authority" label="权限" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column label="操作" align="center" :width="200">
            <template slot-scope="scope">
              <el-button @click="vivew(scope.row)" type="text">查看</el-button>
              <template v-if="!$store.state.checkLevel">
                <el-button
                  v-if="platform && scope.row._authority != '私人'"
                  @click="pushUpTrigger(scope.row)"
                  type="text"
                >推上级</el-button>
                <el-button
                  v-if="$store.state.platformInformation"
                  @click="pushLow(scope.row)"
                  type="text"
                >推下级</el-button>
              </template>
              <el-button
                @click="delete_confirm(scope.row)"
                type="text"
                class="delete-button"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <div class="table-pagination" v-show="total_number > 10">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="listParam.page_index"
            :page-sizes="[10, 20, 30, 40, 50, 100]"
            :page-size="listParam.page_size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total_number"
          ></el-pagination>
        </div>
      </div>
    </div>
    <!-- 推下级begin -->
    <el-dialog
      title="推下级"
      :visible.sync="pushLowFlag"
      v-if="pushLowFlag"
      width="600px"
      :close-on-click-modal="false"
    >
      <push-low-win @listenPushLow="getPushLowList"></push-low-win>
      <span slot="footer" class="dialog-footer">
        <el-button @click="pushActive()">取 消</el-button>
        <el-button type="primary" @click="pushActive(1)">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 推下级end -->
  </div>
</template>
<script>
import pushLowWin from "@/components/common/push-low-win";
export default {
  data() {
    return {
      loading: {
        status: false,
        txt: "获取数据...",
      },
      listData: [],
      listParam: {
        study_section_id: null,
        grade_id: null,
        subject_id: null,
        version_id: null,
        source_id: null,
        keyword: "",
        page_index: 1,
        page_size: 10,
        data_type: 2,
        status: "1, 2, 3",
        authority: 1,
      },
      orgLevel2Param: {
        //获取学段参数
        level: 2,
        pid: null,
        direction: "balance",
      },
      orgLevel3Param: {
        //获取年纪参数
        level: 3,
        pid: null,
        direction: "balance",
      },
      orgLevel2: [{ id: null, name: "全部学段" }],
      orgLevel3: [{ id: null, name: "全部年级" }],
      orgSubject: [{ id: null, name: "全部科目" }],
      subjectParam: {
        //获取科目,版本，参数
        type: 2,
        org_id: null,
        status: 1,
      },
      total_number: 0,
      sourceData: [],
      platform_id: null,
      platformList: [] /*学校/教育局列表 */,
      platform: "" /*可推送上级 */,
      pid: "" /*可推送上级id */,
      pushLowFlag: false /*推下级弹窗 */,
      pushLowObj: null /*推下级对象 */,
      pushLowNames: ["1", "2"] /*获取下级列表参数 */,
      schoolList: [] /*学校列表 */,
      selSchoolList: [] /*选择的下级列表 */,
      ids: [] /*存放被选中的视频列表 */,
      pushLowStatus: null /*记录是否批量推下级*/,
      pushUpStatus: null /*记录是否批量推上级 */,
    };
  },
  created() {
    this.getSource();
    this.getOrgLevel2();
    this.getOrgLevel3();
    this.getSubject();
    this.getList();
    if (this.$store.state.platformInformation) {
      this.get_canseeposition();
    }
    if (this.$store.state.checkLevel) {
      this.platform_parent();
    }
  },
  watch: {
    "listParam.study_section_id": {
      handler(newVal, oldVal) {
        this.listParam.grade_id = null;
        this.listParam.subject_id = null;
        this.listParam.source_id = null;

        this.orgLevel3Param.pid = newVal;
        this.getOrgLevel3();

        this.subjectParam.org_id = newVal;
        this.getSubject();
      },
    },
    "listParam.grade_id": {
      handler(newVal, oldVal) {
        this.listParam.subject_id = null;
        this.listParam.source_id = null;

        this.subjectParam.org_id = newVal;
        this.getSubject();
      },
    },
    "listParam.subject_id": {
      handler(newVal, oldVal) {
        this.listParam.source_id = null;
      },
    },
  },
  methods: {
    // 根据权限判断是否可勾选
    selectable(row, index) {
      if (row._authority == "私人") {
        return false;
      } else {
        return true;
      }
    },
    deleteMuilt_confirm() {
      if (this.ids.length <= 0) {
        this.$message.warning("请选择要删除的项");
        return false;
      }
      let yes = (data) => {
        this.deleteAll();
      };
      let cancel = (data) => {};
      this.e_confirm({
        yes: yes,
        cancel: cancel,
        txt: "确定要删除选中的项吗？",
      });
    },
    deleteAll() {
      let params = {
        id: this.ids.join(","),
        status: 0,
      };
      let success = (data) => {
        this.$message.success(data.msg);
        this.getList();
      };
      this.$ajax.delete("api/test/test", {
        params: params,
        func: { success: success },
      });
    },
    searchFunc() {
      this.listParam.page_index = 1;
      this.getList();
    },
    reset() {
      this.orgLevel2Param = {
        //获取学段参数
        level: 2,
        pid: null,
        direction: "balance",
      };
      this.orgLevel3Param = {
        //获取年纪参数
        level: 3,
        pid: null,
        direction: "balance",
      };
      this.subjectParam = {
        //获取科目,版本，参数
        type: 2,
        org_id: null,
        status: 1,
      };
      this.listParam = {
        platform_id: null,
        study_section_id: null,
        grade_id: null,
        subject_id: null,
        source_id: null,
        keyword: "",
        page_index: 1,
        page_size: 10,
        data_type: 2,
        status: "1, 2, 3",
        authority: 1,
      };
      this.getList();
    },
    // 试卷查看
    vivew(row) {
      this.$router.push("/resources/exam/examinationPaper-view/" + row.id);
    },
    // 序号
    typeIndex(index) {
      return (
        index + (this.listParam.page_index - 1) * this.listParam.page_size + 1
      );
    },
    getStatus(val) {
      return this.statusArr[parseInt(val)];
    },
    getClassType(val) {
      return this.classTypeArr[parseInt(val)];
    },

    getSource() {
      let success = (data) => {
        this.sourceData = data.data;
        this.sourceData.unshift({ id: null, name: "全部类型" });
      };
      //执行请求，并将回调方法变成config成员以便鉴权过期可以执行
      this.$ajax.get("api/test/test_source", {
        params: {},
        func: { success: success },
      });
    },
    getList() {
      this.loading.status = true;
      let success = (data) => {
        this.loading.status = false;
        this.listData = data.data.data;
        this.total_number = data.data.data_sum;
      };
      let fail = (data) => {
        this.loading.status = false;
        this.$message.error(data.msg);
      };
      //执行请求，并将回调方法变成config成员以便鉴权过期可以执行
      this.$ajax.get("api/test/test", {
        params: this.listParam,
        func: { success: success, fail: fail },
      });
    },
    //获取学段
    getOrgLevel2() {
      let success = (data) => {
        this.orgLevel2 = data.data;
        this.orgLevel2.unshift({ id: null, name: "全部学段" });
      };
      if (!this.$store.state.platformInformation &&this.$store.state.checkLevel) {
        let params = {
          level: 2,
          app_code: sessionStorage.getItem("CheckSchoolAppCode"),
        };
        this.$ajax.get("api/network/get_platform_org", {
          params,
          func: { success },
        });
      } else {
        this.$ajax.get("api/org/org", {
          params: this.orgLevel2Param,
          func: { success: success },
        });
      }
    },
    //获取年级
    getOrgLevel3() {
      let success = (data) => {
        this.orgLevel3 = data.data;
        this.orgLevel3.unshift({ id: null, name: "全部年级" });
      };
      if (!this.$store.state.platformInformation &&this.$store.state.checkLevel) {
        let params = {
          level: 3,
          app_code: sessionStorage.getItem("CheckSchoolAppCode"),
        };
        this.$ajax.get("api/network/get_platform_org", {
          params,
          func: { success },
        });
      } else {
        this.$ajax.get("api/org/org", {
          params: this.orgLevel3Param,
          func: { success: success },
        });
      }
    },
    //获取科目
    getSubject() {
      let success = (data) => {
        this.orgSubject = data.data;
        this.orgSubject.unshift({ id: null, name: "全部科目" });
      };
      if (this.$store.state.checkLevel) {
          this.subjectParam.app_code = sessionStorage.getItem("CheckSchoolAppCode"),
          this.$ajax.get("api/newlevel/platform_subject", {
            params: this.subjectParam,
            func: { success },
          });
      } else {
        this.$ajax.get("api/cate/subject", {
          params: this.subjectParam,
          func: { success: success },
        });
      }
    },

    delete_confirm(obj) {
      let yes = (data) => {
        this.delete(obj.id, 0);
      };
      let cancel = (data) => {};
      this.e_confirm({
        yes: yes,
        cancel: cancel,
        txt: "确定将 " + obj.name + " 删除吗？",
      });
    },
    delete(id, status) {
      let success = (data) => {
        this.$message.success(data.msg);

        if (this.listData.length - 1 === 0) {
          if (this.listParam.page_index > 1) {
            this.listParam.page_index--;
          } else {
            this.listParam.page_index = 1;
          }
        }

        this.getList();
      };
      this.$ajax.delete("api/test/test", {
        params: { id: id, status: status },
        func: { success: success },
      });
    },
    handleSizeChange(val) {
      this.listParam.page_index = 1;
      this.listParam.page_size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listParam.page_index = val;
      this.getList();
    },
    get_canseeposition() {
      /*获取学校/教育局列表 */
      const success = (res) => {
        this.platformList = res.data;
        console.log(this.platformList);
      };
      if (
        !this.$store.state.platformInformation &&
        this.$store.state.checkLevel
      ) {
        let params = {
          level: 2,
          app_code: sessionStorage.getItem("CheckSchoolAppCode"),
        };
        this.$ajax.get("api/network/get_platform_org", {
          params,
          func: { success },
        });
      } else {
        this.$ajax.get("api/level/get_canseeposition_school", {
          params: {},
          func: { success: success },
        });
      }
    },
    platform_parent() {
      /*获取可推送上级 */
      const success = (res) => {
        if (!is_empty(res.data)) {
          this.platform = res.data.name;
          this.pid = res.data.id;
        }
      };
      this.$ajax.get("api/level/platform_parent", {
        params: {},
        func: { success: success },
      });
    },
    pushLow(data) {
      /*推下级 */
      this.pushLowObj = data;
      this.pushLowStatus = null;
      this.pushLowFlag = true;
    },
    pushActive(flag) {
      /*推下级执行 */
      if (flag) {
        if (this.selSchoolList.length <= 0) {
          this.$message.error("请选择要推送的下级");
          return;
        }
        let params = {
          id: [],
          res_type: "test",
          school_id: "",
          push_type: 2,
        };
        if (this.pushLowStatus) {
          params.id = this.ids;
        } else {
          params.id.push(this.pushLowObj.id);
        }
        let arr = [];
        this.selSchoolList.forEach((item) => {
          arr.push(item.id);
        });
        params.school_id = arr.join();
        const success = (res) => {
          this.pushLowHide();
          this.ids = [];
          this.getList();
        };
        this.$ajax.post("api/level/push_data", params, {
          func: { success: success },
        });
      } else {
        this.pushLowHide();
      }
    },
    pushLowAll() {
      /*批量推下级 */
      if (this.ids.length <= 0) {
        this.$message.error("请选择要推送的选项");
        return;
      }
      this.pushLowStatus = 1;
      this.pushLowFlag = true;
    },
    getPushLowList(list) {
      /*获取组件传递列表 */
      this.selSchoolList = list;
    },
    pushLowHide() {
      /*推下级窗口隐藏 */
      this.pushLowObj = null;
      this.pushLowFlag = false;
    },
    pushUp(id) {
      let params = {
        id: id,
        res_type: "test",
        school_id: this.pid,
        push_type: 1,
      };
      const success = (res) => {
        this.$message.success(res.msg);
        this.ids = [];
        this.getList();
      };
      this.$ajax.post("api/level/push_data", params, {
        func: { success: success },
      });
    },
    pushUpTrigger(data) {
      let title = `是否要将资源推送至${this.platform}？（需经过审核）`;
      this.$confirm(title, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (data) {
            let ids = [data.id];
            this.pushUp(ids);
          } else {
            this.pushUp(this.ids);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消推送",
          });
        });
    },
    pushUpAll() {
      /*批量推上级 */
      if (this.ids.length <= 0) {
        this.$message.error("请选择要推送的选项");
        return;
      }
      this.pushUpTrigger();
    },
    toLink(link) {
      this.$router.push(link);
    },
    handleSelectionChange(val) {
      this.ids = [];
      if (val.length > 0) {
        val.forEach((item) => {
          this.ids.push(item.id);
        });
      }
    },
  },
  components: {
    pushLowWin,
  },
};
</script>
<style lang="scss">
.zh-examinationPaper {
}
</style>